﻿/*!
		JQuery XML Store / Shop - Shopping Cart
		Created by LivelyWorks - http://livelyworks.net

	*/
$(document).ready(function(){
    var t="",k="$",e="USD",j="",p=1,D=1,x=0,c="xml_jquery_store_"+window.location.hostname,n=new Array(),a=new Array(),o="",i="",s=1,l="",u="",B=new Number(0),z=false,h=new Date(),b=false;
    $.ajax({
        type:"GET",
        url:"configData.xml?file="+h.getTime(),
        dataType:"xml",
        success:function(E){
            var F=$(E).find("configuration")[0];
            t=$(F).attr("logoImage");
            if($(F).attr("currencySymbol")){
                k=$(F).attr("currencySymbol")
            }
            if($(F).attr("currency")){
                e=$(F).attr("currency")
            }
            if($(F).attr("businessEmail")){
                j=$(F).attr("businessEmail")
            }
            if($(F).attr("UsePaypal")){
                p=parseInt($(F).attr("UsePaypal"))
            }
            if($(F).attr("UseSubmitOrder")){
                D=parseInt($(F).attr("UseSubmitOrder"))
            }
            if($(F).attr("ShippingCharges")){
                x=parseFloat($(F).attr("ShippingCharges"))
            }
            $("#site_logo").attr("src",t);
            if(p==0){
                $("#checkout_with_paypal").hide()
            }
            if(D==0){
                $("#checkout_submit_order").hide()
            }
            C();
            $.ajax({
                type:"GET",
                url:"productsData.xml?file="+h.getTime(),
                dataType:"xml",
                success:function(G){
                    var I=1;
                    var K=1;
                    var H="";
                    var J=0;
                    $(G).find("category").each(function(){
                        var M=I;
                        var L=$(this).attr("categoryName");
                        $("<li></li>").html('<a href="#" class="cat_data_id_'+I+'">'+L+"</a>").appendTo("#categories_list");
                        $('<div class="items" id="category_'+M+'"></div>').html('<a href="#"> <h2>'+I+": "+L+"</h2></a>").appendTo("#products");
                        $(this).find("product").each(function(){
                            var V=$(this).attr("productName");
                            var Q=$(this).attr("thumbPath");
                            var U=new Number($(this).attr("productPrice")).toFixed(2);
                            var T=$(this).attr("productID");
                            var R=I+"_"+K;
                            var N=$(this).find("details").text();
                            var O='<label class="control-label" for="siz_'+R+'">Select Size: </label> <select class="input-small" id="siz_'+R+'">';
                            $(this).find("size").each(function(){
                                O+="<option>"+$(this).text()+"</option>"
                            });
                            O+="</select>";
                            var S='<label class="control-label" for="col_'+R+'">&nbsp; Select Color: </label> <select class="input-small" id="col_'+R+'">';
                            $(this).find("color").each(function(){
                                S+="<option>"+$(this).text()+"</option>"
                            });
                            S+="</select>";
                            var P = '<div id="product_' + R + '" class="product_details_modal modal hide fade">' +
                                        '<div class="modal-header">' +
                                            '<a class="close" data-dismiss="modal" >&times;</a>' +
                                            '<div class="caption">' +
                                                '<h3 id="product_name_' + R + '">' + V + "</h3>" +
                                                "<h4><small> Category: <strong>" + L + "</strong> Product ID: <strong>" + T + '</strong> </small><strong class="price_color"> &nbsp; Price: ' + k + " " + U + '</strong></h4>'+
                                            '</div>' +
                                        '</div>' +
                                        '<div class="modal-body">' + N + '</div>' +
                                        '<div class="modal-footer">' +
                                            '<div></div><br />' +
                                            '<div class="form-inline">' + O + S + '<a href="#" class="btn" data-dismiss="modal" >Close</a><a href="#" id="btn_' + R + '" class="btn btn-warning add_to_cart_btn">Add to <i class="icon-shopping-cart icon-white"></i></a></div>' +
                                        '</div>' +
                                    '</div>';
                            $('<li class="span2 product_item" data-id="prod_data_id_' + R + '" data-type="cat_data_id_' + I + '" id="product_item_' + R + '"></li>').html(
                                '<div class="thumbnail">' +
                                    '<div class="thumb_holder">' +
                                        '<a data-toggle="modal" href="#product_' + R + '" ><img src="' + Q + '" alt="' + V + '"/></a>' +
                                    '</div>' +
                                    '<div class="caption">' +
                                        '<div class="product_sum"><h4>' + V + '</h4><h3 class="price_color">' + k + " " + U + "</h3></div>" +
                                        P +
                                        '<p><a class="btn btn-warning" data-toggle="modal" href="#product_' + R + '" ><i class="icon-zoom-in icon-white"></i> View details</a></p>' +
                                    '</div>' +
                                '</div>').appendTo("#products_container");
                            
                            n["btn_"+R]={
                                productName:V,
                                productPrice:U,
                                uProductID:R,
                                productID:T
                            };
                            
                            $("#btn_"+R).live("click",w);
                            $("#product_item_"+R).live("mouseover",function(){
                                $("#products_breadcrumb").html(L+" &raquo; "+V)
                            });
                            $("#product_item_"+R).live("mouseout",function(){
                                $("#products_breadcrumb").html(L)
                            });
                            $("#siz_"+R).live("change",function(){
                                v(R)
                            });
                            $("#col_"+R).live("change",function(){
                                v(R)
                            });
                            K++;
                            v(R)
                        });
                        I++
                    });
                    $("#shopping_cart_loader").hide();
                    m($(".product_item .thumbnail"));
                    l=$("#products_container");
                    u=l.clone()
                }
            })
        }
    });
    var r=$("#categories_list li.active_category a").attr("class");
    $("#categories_list li a").live("click",function(F){
        $("#products_breadcrumb").html($(this).text());
        $("#categories_list li").removeClass("active_category");
        var E=$(this).attr("class");
        $(this).parent().addClass("active_category");
        if(E=="all"){
            var G=u.find("li")
        }else{
            var G=u.find("li[data-type="+E+"]")
        }
        l.quicksand(G,{
            duration:400,
            easing:"easeInOutQuad"
        },function(){
            C()
        });
        return false
    });
    var g=$.cookie(c);
    if(g!=null){
        a=$.parseJSON(g)
    }
    function w(){
        var H=$(this).attr("id");
        var E=H.substring(4);
        var M=n[H].productName;
        var G=n[H].productPrice;
        var K=n[H].productID;
        var J=$("#siz_"+E+" option:selected").val();
        var I=$("#col_"+E+" option:selected").val();
        var F=1;
        for(var L in a){
            if(M==a[L].productName&&J==a[L].productSize&&I==a[L].productColor){
                F=a[L].productQty++;
                C();
                return false
            }
        }
        a.push({
            productName:M,
            productPrice:G,
            productSize:J,
            productColor:I,
            productQty:1,
            uProductID:E,
            productID:K
        });
        s++;
        C();
        z=false;
        return false
    }
    function C(){
        o="";
        i="";
        B=0;
        var E=0;
        for(var F in a){
            var G=a[F];
            B=B+(G.productPrice*G.productQty);
            o+='<tr id="cart_row_'+F+'">';
            o+="<td>"+G.productName+"</td>";
            o+="<td>"+G.productSize+"</td>";
            o+="<td>"+G.productColor+"</td>";
            o+="<td>"+k+" "+G.productPrice+"</td>";
            o+='<td><input type="number" step="1" min="1" value="'+G.productQty+'" class="input-small span1 cart_product_qty"/></td>';
            o+="<td>"+k+" "+(G.productPrice*G.productQty).toFixed(2)+"</td>";
            o+='<td><a href="#" id="delete_prod_id'+F+'" class="close delete_product_from_cart" title="Delete">&times;</a></td>';
            o+="</tr>";
            v(G.uProductID);
            E+=G.productQty
        }
        $.cookie(c,$.toJSON(a));
        $("#shopping_cart_data").html(o);
        var H=B>0?x:0;
        i+="Base Total: "+(B).toFixed(2)+" + Shipping: "+k+" "+(H).toFixed(2)+" = <strong>"+k+" "+(B+H).toFixed(2)+" "+e+"</strong>";
        $("#shopping_cart_total").html(i);
        $("#show_cartBtn").html(E+"</strong> Item(s) of <strong>"+k+" "+(B).toFixed(2)+" "+e+' </strong>in your <i class="icon-shopping-cart"></i><strong>');
        alert('Sản phẩm được thêm vào giỏ hàng');
        if(B<=0){
            $("#checkout_with_paypal").addClass("disabled");
            $("#checkout_submit_order").addClass("disabled")
        }else{
            $("#checkout_with_paypal").removeClass("disabled");
            $("#checkout_submit_order").removeClass("disabled")
        }
        return false
    }
    function v(J){
        var G=J,H="btn_"+G,I=$("#product_name_"+G).text(),K=$("#siz_"+G+" option:selected").val(),F=$("#col_"+G+" option:selected").val();
        for(var E in a){
            if(I==a[E].productName&&K==a[E].productSize&&F==a[E].productColor){
                $("#btn_"+a[E].uProductID).html('Update <i class="icon-shopping-cart icon-white"></i>: '+a[E].productQty);
                return false
            }
        }
        $("#btn_"+G).html('Add to <i class="icon-shopping-cart icon-white"></i>');
        return false
    }
    $(".delete_product_from_cart").live("click",y);
    $(".cart_product_qty").live("change",f);
    function f(){
        var E=$(this).parent().parent().attr("id"),F=E.substring(9);
        if($(this).val()<=0||!Number($(this).val())){
            $(this).val(1)
        }
        a[F].productQty=Math.round($(this).val());
        C();
        return false
    }
    function y(){
        var F=$(this).attr("id");
        var E=F.substring(14);
        $("#btn_"+a[E].uProductID).html('Add to <i class="icon-shopping-cart icon-white"></i>');
        a.splice(E,1);
        C();
        return false
    }
    $("#checkout_with_paypal").live("click",A);
    $("#checkout_submit_order").live("click",function(){
        if(B<=0){
            return false
        }
        $("#submit_order_form_holder").modal("show")
    });
    $("#submit_order_btn").live("click",d);
    $("#sof_email").live("change",function(){
        q()
    });
    function q(){
        var E=$("#sof_email").val();
        if(E==""){
            return false
        }
        var F=/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        if(!F.test(E)){
            $("#submit_order_btn").addClass("disabled");
            return false
        }else{
            $("#submit_order_btn").removeClass("disabled");
            return true
        }
    }
    $("#order_submit_status").on("hidden",function(){
        z=false
    });
    $("#submit_order_form_holder").on("hidden",function(){
        if(z==false){
            $("#cart_holder").modal("show")
        }
    });
    $("#submit_order_form_holder").on("show",function(){
        $("#cart_holder").modal("hide")
    });
    function A(){
        if(B<=0){
            return false
        }
        var G="https://www.paypal.com/cgi-bin/webscr?cmd=_cart&upload=1&charset=utf-8&currency_code="+e+"&business="+j+"&handling_cart="+x;
        var E=1;
        for(var F in a){
            G+="&item_name_"+E+"="+a[F].productName+" Size: "+a[F].productSize+" Color: "+a[F].productColor;
            G+="&item_number_"+E+"="+a[F].productID;
            G+="&amount_"+E+"="+a[F].productPrice;
            G+="&quantity_"+E+"="+a[F].productQty;
            E++
        }
        window.open(G);
        return false
    }
    function d(){
        if(B<=0){
            return false
        }
        if(q()==false){
            return false
        }
        if(b==true){
            return false
        }
        b=true;
        $("#submit_order_btn").addClass("disabled");
        $("#submit_order_btn").text("Processing...");
        var G="cartMailer.html?currency_code="+e+"&business="+j+"&value=USD&handling_cart="+x;
        var E=1;
        for(var F in a){
            G+="&item_name_"+E+"="+a[F].productName+" Size: "+a[F].productSize+" Color: "+a[F].productColor;
            G+="&item_number_"+E+"="+a[F].productID;
            G+="&amount_"+E+"="+a[F].productPrice;
            G+="&quantity_"+E+"="+a[F].productQty;
            E++
        }
        var H=$("#submit_order_form").serialize();
        G+="&cartLength="+E+"&"+H;
        $.post(G,function(J){
            z=true;
            if(J!="mailSentSuccess=1"){
                $("#order_status_text").html("Placing order is Failed. Please Try Again!!");
                $("#submit_order_form_holder").modal("hide");
                $("#order_submit_status").modal("show")
            }else{
                $("#order_status_text").html("Your order has been Placed!!");
                $("#submit_order_form_holder").modal("hide");
                $("#order_submit_status").modal("show");
                for(var I in a){
                    $("#btn_"+a[I].uProductID).html('Add to <i class="icon-shopping-cart icon-white"></i>')
                }
                a=[];
                C()
            }
            b=false;
            $("#submit_order_btn").removeClass("disabled");
            $("#submit_order_btn").text("Submit Order")
        });
        return false
    }
    $("a.topLink").click(function(){
        $("html, body").animate({
            scrollTop:"0px"
        },{
            duration:600,
            easing:"swing"
        });
        return false
    });
    function m(E){
        tallest=0;
        E.each(function(){
            thisHeight=$(this).height();
            if(thisHeight>tallest){
                tallest=thisHeight
            }
        });
        if(tallest<220){
            E.height(220)
        }else{
            E.height(tallest)
        }
    }
    C()
});